<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 用户管理
 *
 * @icon fa fa-circle-o
 */
class Company extends Backend
{
    
    /**
     * Userinfo模型对象
     * @var \app\admin\model\Userinfo
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Userinfo;

    }

    /**
     * 公司详情展示
     */
    public function companyinfo(){
        $result = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> find();
        $constructionLength = mb_strlen($result['construction'], 'utf8');
        $this -> assign('length', $constructionLength);
        $this -> assign('companyinfo', $result);
        return view('companyinfo');
    }
    /**
     * 侧边栏信息编辑
     */
    public function companyedit(){

        if($this -> request -> isPost()){
            $input = input('post.');
            $result = model('userinfo') -> editUserinfo($input);
            if($result){
                return json(['status' => 1, 'info' => '更新成功！']);
            }
            return json(['status' => 0, 'info' => '更新失败！']);
        }

        $userinfo = model('userinfo') -> where(['uid' => session('userinfo:id')]) -> find();
        $this -> assign('userinfo', $userinfo);
        return view();
    }
    //七牛上传
    public function qiniu() {
        $file = $this -> request -> file('myfile');
        $filePath = $file -> getInfo('tmp_name');

        $ext = pathinfo($file -> getInfo('name'), PATHINFO_EXTENSION);
        $key = 'upload/'.date('Ymdhis').substr(md5($file -> getRealPath()), 0, 5). rand(0, 999).'.'.$ext;

        vendor('qiniu.autoload');
        $accessKey = config('ACCESSKEY');
        $secretKey = config('SECRETKEY');
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        $bucket = config('BUCKET');
        $domain = config('domain');
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        $uploaderMgr = new \Qiniu\Storage\UploadManager();
        //调用putFile方法上传文件
        list($ret, $err) = $uploaderMgr -> putFile($token, $key, $filePath);
        if($err !== null) {
            return json(['status' => 0, 'info' => $err]);
        }else {
            return json(['status' => 1, 'info' => '上传成功！', 'data' => '//'.$domain.'/'.$ret['key']]);
        }
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
